package com.activiti.action;

import java.util.Date;
import java.util.List;

import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.repository.ProcessDefinition;

import com.activiti.biz.BaoxiaoApproveBiz;
import com.activiti.biz.BaoxiaoBiz;
import com.activiti.po.ActIdUser;
import com.activiti.po.Baoxiao;
import com.activiti.po.BaoxiaoApprove;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Manage extends ActionSupport {
	private String taskId;
	private String executionId;
	private RuntimeService runtimeService;
	private RepositoryService repositoryService;
	private TaskService taskService;
	private ActivityImpl currentAct;
	private Baoxiao baoxiao;
	private String baoxiaoId;
	private BaoxiaoBiz baoxiaoBiz;
	private BaoxiaoApproveBiz baoxiaoApproveBiz;
	private String result;
	private String memo;
	private HistoryService historyService;
	
	public String toVerify() {
		Short baoxiaoId=(Short)runtimeService.getVariable(executionId, "baoxiaoId");
		baoxiao=baoxiaoBiz.getBaoxiao(baoxiaoId);
		return SUCCESS;
	}
	
	public String verify(){
		String currentUsername=(String)ActionContext.getContext().getSession().get("username");
		taskService.claim(taskId, currentUsername);
		BaoxiaoApprove baoxiaoApprove=new BaoxiaoApprove();
		baoxiaoApprove.setActIdUser(new ActIdUser(currentUsername));
		baoxiaoApprove.setBaoxiao(new Baoxiao(Short.valueOf(baoxiaoId)));
		baoxiaoApprove.setDate(new Date());
		baoxiaoApprove.setMemo(memo);
		baoxiaoApprove.setResult(result);
		
		baoxiaoApproveBiz.addBaoxiaoApprove(baoxiaoApprove);
		taskService.complete(taskId);
		System.out.println("流程结束，reslut是"+result);
		return SUCCESS;
	}
	
	public String showImg(){
		ProcessDefinition processDefinition=repositoryService.createProcessDefinitionQuery()
			.processDefinitionKey("Baoxiao").latestVersion().singleResult();
		
		List<ActivityImpl> activityImpls=(List<ActivityImpl>) ((RepositoryServiceImpl)repositoryService)
			.getDeployedProcessDefinition(processDefinition.getId()).getActivities();
		
		
		ExecutionEntity execution=(ExecutionEntity)runtimeService.createExecutionQuery().executionId(executionId).singleResult();
		if(execution==null) return "failure";
		for(ActivityImpl activityImpl:activityImpls){
			if(activityImpl.getId().equals(execution.getActivityId()))
				currentAct=activityImpl;
		}
		return SUCCESS;
	}
	
	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getExecutionId() {
		return executionId;
	}

	public void setExecutionId(String executionId) {
		this.executionId = executionId;
	}

	public RuntimeService getRuntimeService() {
		return runtimeService;
	}

	public void setRuntimeService(RuntimeService runtimeService) {
		this.runtimeService = runtimeService;
	}

	public RepositoryService getRepositoryService() {
		return repositoryService;
	}

	public void setRepositoryService(RepositoryService repositoryService) {
		this.repositoryService = repositoryService;
	}

	public ActivityImpl getCurrentAct() {
		return currentAct;
	}

	public void setCurrentAct(ActivityImpl currentAct) {
		this.currentAct = currentAct;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public Baoxiao getBaoxiao() {
		return baoxiao;
	}

	public void setBaoxiao(Baoxiao baoxiao) {
		this.baoxiao = baoxiao;
	}

	public BaoxiaoBiz getBaoxiaoBiz() {
		return baoxiaoBiz;
	}

	public void setBaoxiaoBiz(BaoxiaoBiz baoxiaoBiz) {
		this.baoxiaoBiz = baoxiaoBiz;
	}

	public String getBaoxiaoId() {
		return baoxiaoId;
	}

	public void setBaoxiaoId(String baoxiaoId) {
		this.baoxiaoId = baoxiaoId;
	}

	public BaoxiaoApproveBiz getBaoxiaoApproveBiz() {
		return baoxiaoApproveBiz;
	}

	public void setBaoxiaoApproveBiz(BaoxiaoApproveBiz baoxiaoApproveBiz) {
		this.baoxiaoApproveBiz = baoxiaoApproveBiz;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}
	
	
}
